<!--
        Copyright (c) 2023, LXMS (lxinet.cn).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<template>
  <div class="loginBox">
    <div class="loginCon">
      <div class="title">欢迎登录系统</div>
      <lx-form ref="formNode" :formConfig="formConfig" :formData="formData"></lx-form>
      <a-button type="primary" block size="large"  :loading="loading"  @click="loginInit()">登录</a-button>
    </div>
  </div>
</template>
<script>
  import lxForm from "@components/baseComponents/lxForm/index.vue"
  export default {
    name: "Login",
    components: {
      lxForm
    },
    mixins:[],
    data() {
      return {
        publicKey: "",
        formData:{
          name: "",
          password: ""
        },
        formConfig:[
          {
            component: "a-input",
            key: "name",
            colProps:{
              span:24
            },
            props: {
              placeholder:"用户名",
              size:"large"
            },
            prefix:"userOutlined",
            rules:[{ required: true, message: '请输入用户名' ,trigger:"blur"}]
          },
          {
            component: "a-input-password",
            key: "password",
            props: {
              placeholder:"密码",
              size:"large"
            },
            prefix:"LockOutlined",
            rules:[{ required: true, message: '请输入用户名',trigger:"blur" }],
            colProps:{
              span:24
            }
          },
        ],
        loading:false
      };
    },
    methods: {
      getPublicKey(){
        this.loading=true;
        this.$request({
          url: "/system/getPublicKey",
          method: "get",
        }).then((res) => {
          console.log(res)
          this.publicKey = res.data;
        }).finally(()=>{
          this.loading=false;
        })
      },
      loginInit(){
        this.$refs.formNode.$refs.form.validateFields().then((res)=>{
          this.loginImpl();
        })

      },
      loginImpl(){
        this.loading=true;
        this.$request({
          url: "/system/auth/login",
          method: "post",
          data: {
            ...this.formData,
            password: this.$encrypt.rasEncrypt(this.formData.password, this.publicKey),
          },
        }).then((res) => {
          this.$localStorage.set("token",res.data.token);
          this.$router.push("/index")
        }).finally(()=>{
          this.loading=false;
        })
      },
    },
    mounted() {
      this.getPublicKey();
    }
  }
</script>
<style lang="less" scoped>
  .loginBox{
    position: fixed;
    width: 100%;
    height: 100%;
    background: url("@img/login_bg.png")no-repeat top left;
    background-size:cover;
    .loginCon{
      background: #FFFFFF;
      position: fixed;
      left: 55%;
      top:50%;
      padding: 50px 50px 30px 50px;
      width: 500px;
      height: 350px;
      margin-top: -200px;
      .base-shadow();
      .base-radius(12px);
      .title{
        text-align: center;
        padding-bottom: 30px;
        font-size: 24px;
      }
      .ant-btn{
        margin-top: 5px;
      }
    }
  }


</style>